package com.gamebox.view;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.gamebox.dao.GameDao;
import com.gamebox.javaBean.Game;
import com.gamebox.javaBean.Remark;
import com.gamebox.javaBean.User;

public class GameDetailDialog extends JDialog{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2845706587252420927L;
	private Game game;
	public GameDetailDialog(final Game game,final User user){
		final GameDao gameDao = new GameDao();
		gameDao.updateGameClickNum(game);
		this.game = game;
		this.setTitle("游戏详情");
		this.setBounds(100, 100, 600, 600);
		Container con=this.getContentPane();  
		con.setLayout(null);
		JLabel nameLabel=new JLabel("游戏名称:");
		JLabel name = new JLabel(game.getName());
		nameLabel.setBounds(100,10, 80, 30);
		name.setBounds(200, 10, 100, 30);
		JButton startBt = new JButton("开始游戏");
		startBt.setBounds(350, 10, 100, 30);
		startBt.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				gameDao.updateGamePlayerNum(game);
				gameDao.addPlayRecord(user.getId(), game.getId());
				String path = game.getPath();
				Runtime run = Runtime.getRuntime();
				try {
					Process p = run.exec("java -jar "+path);
					BufferedInputStream in = new BufferedInputStream(p.getErrorStream());  
			        BufferedReader inBr = new BufferedReader(new InputStreamReader(in));  
			        String lineStr;  
			         while ((lineStr = inBr.readLine()) != null)  
			                //获得命令执行后在控制台的输出信息  
			                System.out.println(lineStr);// 打印输出信息  
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		JLabel markLabel=new JLabel("游戏得分:");
		JLabel mark=new JLabel(game.getScore()+"");
		markLabel.setBounds(100, 50, 100, 30);
		mark.setBounds(200, 50, 100, 30);
		JLabel myMark=new JLabel("我来打分:");
		final JTextField markText = new JTextField(15);
		myMark.setBounds(100, 90, 100, 30);
		markText.setBounds(200, 90, 100, 30);
		JButton confirmMarkBt = new JButton("确认");
		confirmMarkBt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String mark = markText.getText();
				gameDao.addMark(user.getId(),game.getId(),mark);
				gameDao.updateGameMark(game,Integer.parseInt(mark));
				JOptionPane.showMessageDialog(null, "评分成功");
			}
		});
		confirmMarkBt.setBounds(350, 90, 100, 30);
		JLabel remarkLabel = new JLabel("评论列表");
		remarkLabel.setBounds(100, 140, 100, 30);
		List<Remark> temp = gameDao.getRemarkList(game.getId());
		String s[] = new String[temp.size()];
		for(int i=0;i<temp.size();i++){
			s[i] = temp.get(i).getNickName()+" : "+temp.get(i).getContent();
		}
		JList remarkList = new JList(s);
		remarkList.setBounds(200, 140, 200, 150);
		JLabel myRemarkLabel = new JLabel("我来评论:");
		myRemarkLabel.setBounds(100, 310, 100, 30);
		final JTextField remarkText = new JTextField(15);
		remarkText.setBounds(200, 310, 200, 30);
		JButton remarkBt = new JButton("确认");
		remarkBt.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String remark = remarkText.getText();
				gameDao.addRemark(user.getId(),game.getId(),remark);
				JOptionPane.showMessageDialog(null, "评论成功");
			}
		});
		remarkBt.setBounds(420, 310, 100, 30);
		JLabel playerLabel = new JLabel("谁还玩过:");
		playerLabel.setBounds(100, 360, 100, 30);
		List<User> temp1 = gameDao.getPlayerList(game.getId());
		String s1[] = new String[temp1.size()];
		for(int j=0;j<temp1.size();j++){
			s1[j] = temp1.get(j).getNickName();
		}
		final JList playerList = new JList(s1);
		playerList.setBounds(200, 360, 200, 150);
		playerList.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				if(e.getClickCount()==2){
					String nick = (String) playerList.getSelectedValue();
					User user1 = gameDao.getUserByNick(nick);
					new UserDetailDialog2(user1,user);
				}
			}
		});
		con.add(nameLabel);
		con.add(name);
		con.add(startBt);
		con.add(markLabel);
		con.add(mark);
		con.add(myMark);
		con.add(markText);
		con.add(confirmMarkBt);
		con.add(remarkLabel);
		con.add(remarkList);
		con.add(myRemarkLabel);
		con.add(remarkText);
		con.add(remarkBt);
		con.add(playerLabel);
		con.add(playerList);
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
}
